<?php
#
# Markdown Extra  -  A text-to-HTML conversion tool for web writers
#
# PHP Markdown & Extra
# Copyright (c) 2004-2009 Michel Fortin
# <http://michelf.com/projects/php-markdown/>
#
# Original Markdown
# Copyright (c) 2004-2006 John Gruber
# <http://daringfireball.net/projects/markdown/>
#

require_once('markdownParser.class.php');
require_once('markdownExtendedParser.class.php');
require_once('markdownExtraExtendedParser.class.php');

# Optional class attribute for footnote links and backlinks.
define( 'MARKDOWN_FN_LINK_CLASS',         "" );
define( 'MARKDOWN_FN_BACKLINK_CLASS',     "" );
# Optional title attribute for footnote links and backlinks.
define( 'MARKDOWN_FN_LINK_TITLE',         "" );
define( 'MARKDOWN_FN_BACKLINK_TITLE',     "" );

#
# Global default settings:
#

# Change to ">" for HTML output
define( 'MARKDOWN_EMPTY_ELEMENT_SUFFIX',  " />");

# Define the width of a tab for code blocks.
define( 'MARKDOWN_TAB_WIDTH',     4 );
define( 'MARKDOWN_VERSION',  "1.0.1n" ); # Sat 10 Oct 2009
define( 'MARKDOWNEXTRA_VERSION',  "1.2.4" ); # Sat 10 Oct 2009
define( 'MARKDOWNEXTRAEXTENDED_VERSION',  "0.3" );

function Markdown($text) {
#
# Initialize the parser and return the result of its transform method.
#
	# Setup static parser variable.
	static $parser;
	if (!isset($parser)) {
		$parser = new MarkdownExtendedParser();
	}

	# Transform text using parser.
	return $parser->transform($text);
}

function MarkdownExtended($text, $default_clases = []){
  $parser = new MarkdownExtraExtendedParser($default_clases);
  return $parser->transform($text);
}

function doTOC($text) {
	#
	# Adds TOC support by including the following on a single line:
	#
	# [TOC]
	#
	# TOC Requirements:
	#     * Only headings 2-6
	#     * Headings must have an ID
	#     * Builds TOC with headings _after_ the [TOC] tag

	$toc = '';

	preg_match_all ('/<h([2-6]) id="([0-9a-z_-]+)">(.*?)<\/h\1>/i', $text, $h, PREG_SET_ORDER);
	foreach ($h as &$m) $toc .= str_repeat ("\t", (int) $m[1]-2)."*\t [${m[3]}](#${m[2]})\n";

	$toc_content = Markdown($toc);

	return trim ($toc_content, "\n");
}
